package siafi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import siafi.bean.Concepto;
import siafi.bean.PersonaCentro;
import siafi.bean.Servicio;
import siafi.bean.Vinculacion;
import siafi.db.TerceroDAO;
import siafi.util.Administracion;
import siafi.util.Constantes;

/**
 * Servlet implementation class RecivoServicio
 */
public class ReciboServicio extends HttpServlet {

	private static Logger log = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReciboServicio() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Boolean aut = false;
		if ((Boolean) request.getSession().getAttribute("aut") != null && (Boolean) request.getSession().getAttribute("aut")){
			aut = true;			
		}
		Boolean autEst = false;
		if ((Boolean) request.getSession().getAttribute("autEst") != null && (Boolean) request.getSession().getAttribute("autEst")){
			autEst = true;			
		}
		
		/*if ((Boolean) request.getSession().getAttribute("aut")){
			System.out.println("conectado");			
		}
		*/		
		log.info("Ingresando: " + request.getParameter("documento"));
		log.info("DEsde la IP: " + request.getRemoteAddr());
		Boolean mostrar = Boolean.FALSE;
		PersonaCentro personaCentros = null;
		request.getSession().removeAttribute("servicios");
		if (request.getSession().getAttribute("conceptosM") != null
				&& "siMc".equals((String) request.getSession().getAttribute(
						"conceptosM"))) {
			mostrar = Boolean.TRUE;
			request.getSession().removeAttribute("conceptosM");
			personaCentros = (PersonaCentro) request.getSession().getAttribute(
					"tercero");
		} else {
			personaCentros = TerceroDAO.findByDocumento(request
					.getParameter("documento"));
		}
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">");
		out.println("<html>");
		out.println(" <head>");
		out.println(" <link href=\"./css/coolblue.css\" rel=\"stylesheet\" type=\"text/css\"/>");
		out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
		out.println("  <title>Recibos</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("   <div align=\"center\" id=\"header\">");
		out.println(" <img src=\"./images/upn.jpg\"/>");
		out.println(" </div>");
		// si no se encontro a la persona
		if (personaCentros == null) {
			out.println(" <div align=\"center\" id=\"content\">");
			out.println(" <form action=\"/recibosSIAFI/ReciboServicio\" method=\"post\">");
			out.println("     <table>");
			out.println(" <tr>");
			out.println(" <td colspan=\"2\"><h3>Recibo de Servicio</h3></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"2\" align=\"left\"><h3>Ingrese el n&uacute;mero de documento de identidad</h3></td>");
			out.println("</tr>");
			out.println(" <tr>");
			out.println(" <td colspan=\"2\" align=\"left\"><h4>"
					+ TerceroDAO.personanoencontrada() + "</h4></td>");
			out.println("</tr>");
			out.println("  <tr>");
			out.println(" <td><label>N&uacute;mero de Documento</label></td>");
			out.println(" <td><input type=\"text\" size=\"30\" id=\"documento\" name=\"documento\"/></td>");
			out.println("  </tr>");

			out.println(" <tr>");
			out.println("  <td colspan=\"2\"><button style=\"border-color: transparent;\" type=\"submit\" class=\"button\"><img src=\"images/btn_aceptar.gif\"/></button></td>");
			//out.println("  <td colspan=\"2\"><button type=\"submit\" class=\"button\">Siguiente</button></td>");
			out.println(" </tr>");
			out.println("</table>");
			out.println("</form>");
			out.println("</div>");
		} else {
			request.getSession().setAttribute("tercero", personaCentros);

			List<Servicio> serviciosV = TerceroDAO
					.findServicioVigentes(personaCentros.getNro_documento());
			if (serviciosV != null && serviciosV.size() > 0 && !mostrar) {
				out.println(" <div align=\"center\" id=\"content\">");
				out.println(" <form action=\"/recibosSIAFI/CantidadServicio\" method=\"post\">");
				out.println("     <table>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\"><h3>Recibo de Servicio</h1></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\" align=\"left\"><h4 align=\"left\">"
						+ ((PersonaCentro) request.getSession().getAttribute(
								"tercero")).getNombre() + "</h4></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\" align=\"left\"><h4>Posee estos servicios vigentes. Si desea imprimir alguno seleccionelo y haga clic en \"Siguiente\" de lo contrario seleccione \"Generar nuevo recibo\"</h4></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td><label>Tipo de Servicio: </label></td>");
				out.println(" <td> <select size=\"8\" name=\"vigentes\">");

				if (aut || autEst){
					out.println("  <option value=\"0\">-- Generar nuevo recibo</option> ");			
				}
				
				for (Servicio pc : serviciosV) {
					out.println("  <option value=\"" + pc.getIdu_documento()
							+ "\">" + pc.getConcepto() + " generardo el "
							+ pc.getFec_exp().toString() + " vence el "
							+ pc.getFec_limite().toString() + " valor: "
							+ pc.getVal_subtotal() + "</option> ");
				}

				out.println("</td>");
				out.println("</tr>");
				out.println(" <tr>");				
				out.println("  <td ><button style=\"border-color: transparent\" type=\"button\" onclick=\"javascript:history.back(-1)\" class=\"button\"><img src=\"images/btn_regresar.gif\"/></button></td>");				
				out.println("  <td ><button style=\"border-color: transparent;\" type=\"submit\" ><img src=\"images/btn_aceptar.gif\"/></button></td>");
				out.println("  <td ><a href=\"Logout\"><img alt=\"salir\" src=\"images/btn_terminar.gif\"/> </a></td>");
				out.println(" </tr>");				
				out.println("</table>");
				out.println("</form>");
				out.println("</div>");
			} else {
				if (aut || autEst){
				out.println(" <div align=\"center\" id=\"content\">");
				out.println(" <form action=\"/recibosSIAFI/CantidadServicio\" method=\"post\">");
				out.println("     <table>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\"><h3>Recibo de Servicio</h3></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\" align=\"left\"><h4 align=\"left\">"
						+ ((PersonaCentro) request.getSession().getAttribute(
								"tercero")).getNombre() + "</h4></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\" align=\"left\"><h4>Seleccione el concepto</h4></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td><label>Tipo de Servicio: </label></td>");
				out.println(" <td> <select name=\"centroCosto\">");

				List<Concepto> conceptos = TerceroDAO.findConceptos();

				for (Concepto pc : conceptos) {
					out.println("  <option value=\"" + pc.getCod_concepto()
							+ "\">" + pc.getNom_concepto() + "</option> ");
				}

				out.println("</td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println(" <td><label>Tipo de Vinculación: </label></td>");
				out.println(" <td> <select name=\"vinculacion\">");

				List<Vinculacion> vinculacions = TerceroDAO
						.findVinculacion(((PersonaCentro) request.getSession()
								.getAttribute("tercero")).getCod_pertenencia());

				for (Vinculacion vi : vinculacions) {
					out.println("  <option value=\"" + vi.getCod_vinculacion()
							+ "\">" + vi.getSubdominio() + "</option> ");
				}

				out.println("</select></td>");
				out.println("</tr>");
				out.println(" <tr>");
				out.println("  <td><button style=\"border-color: transparent\" type=\"button\" onclick=\"javascript:history.back(-1)\" class=\"button\"><img src=\"images/btn_regresar.gif\"/></button></td>");
				//out.println("  <td colspan=\"1\"  align=\"right\"><button type=\"button\" onclick=\"javascript:history.back(-1)\" class=\"button\">Anterior</button></td>");
				out.println("  <td><button style=\"border-color: transparent;\" type=\"submit\" class=\"button\"><img src=\"images/btn_aceptar.gif\"/></button></td>");
				out.println("  <td><a href=\"Logout\"><img alt=\"salir\" src=\"images/btn_terminar.gif\"/> </a></td>");
				
				out.println(" </tr>");
				out.println("</table>");
				out.println("</form>");
				out.println("</div>");
				
				
				//1010164573
			}else{
				out.println(" <div align=\"center\" id=\"content\">");
				out.println(" <form action=\"\" method=\"post\">");
				out.println("     <table>");
				out.println(" <tr>");
				out.println(" <td colspan=\"2\"><h3>No tiene Recibos Vigentes</h3></td>");
				out.println("</tr>");
			
				out.println(" <tr>");
				out.println("  <td colspan=\"1\" align=\"right\"><button style=\"border-color: transparent\" type=\"button\" onclick=\"javascript:history.back(-1)\" class=\"button\"><img src=\"images/btn_regresar.gif\"/></button></td>");
				
				out.println(" </tr>");
				out.println("</table>");
				out.println("</form>");
				out.println("</div>");
				
			}
			}
		}
		out.println(" <div align=\"right\" id=\"footer\" style=\"width: 80%\">");
		out.println("<img src=\"./images/SiafiReportes.bmp\" width=\"100px\" height=\"30px\"/>");
		out.println("</div>");

		out.println("</body>");
		out.println("</html>");

		out.close();

	}

}
